package com.adprof.demo.natives.recyclerview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.adprof.demo.android.R;
import com.adprof.sdk.api.AdError;
import com.adprof.sdk.api.NativeAdData;
import com.adprof.sdk.api.NativeAdEventListener;
import com.adprof.demo.natives.viewholders.AdViewHolder;
import com.adprof.demo.natives.viewholders.CardViewHolder;
import com.adprof.demo.natives.viewholders.ImageViewHolder;
import com.adprof.demo.natives.viewholders.NewsViewHolder;
import com.adprof.demo.natives.viewholders.NormalViewHolder;
import com.adprof.demo.natives.viewholders.ProductViewHolder;
import com.adprof.demo.natives.viewholders.VideoViewHolder;
import com.adprof.demo.natives.viewholders.ViewHolderDataBinder;
import com.adprof.demo.utils.ConstantsDemo;
import com.adprof.demo.utils.UIUtil;

import java.util.List;

public class NativeRecyclerAdapter extends RecyclerView.Adapter {
    private static final int ITEM_VIEW_TYPE_NORMAL = 0;

    private static final int ITEM_VIEW_TYPE_AD = 1;

    private static final int ITEM_VIEW_TYPE_CARD = 2;
    private static final int ITEM_VIEW_TYPE_IMAGE = 3;
    private static final int ITEM_VIEW_TYPE_VIDEO = 4;
    private static final int ITEM_VIEW_TYPE_NEWS = 5;

    private final List<NativeAdData> mData;
    private final Activity mActivity;

    public NativeRecyclerAdapter(Activity activity, List<NativeAdData> data) {
        this.mActivity = activity;
        this.mData = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d(ConstantsDemo.LOG_TAG, "-----------onCreateViewHolder-----------");

        switch (viewType) {

            // 广告 视图
            case ITEM_VIEW_TYPE_AD:
                View adView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_ad_native, parent, false);
                return new AdViewHolder(adView);

            case ITEM_VIEW_TYPE_CARD:
                View cardView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_card_content, parent, false);
                return new CardViewHolder(cardView);

            case ITEM_VIEW_TYPE_IMAGE:
                View imageView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_image_content, parent, false);
                return new ImageViewHolder(imageView);

            case ITEM_VIEW_TYPE_VIDEO:
                View videoView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_video_content, parent, false);
                return new VideoViewHolder(videoView);

            case ITEM_VIEW_TYPE_NEWS:
                View newsView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_news_content, parent, false);
                return new NewsViewHolder(newsView);

            default:
                View normalView = LayoutInflater.from(mActivity).inflate(R.layout.listitem_normal, parent, false);
                return new NormalViewHolder(normalView);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Log.d(ConstantsDemo.LOG_TAG, "-----------onBindViewHolder-----------");

        if (holder instanceof AdViewHolder) {
            NativeAdData nativeAdData = mData.get(position);
            final AdViewHolder adViewHolder = (AdViewHolder) holder;
            View view;

            // 这2个一定要设置  且注意顺序
            nativeAdData.setNativeAdMediaListener(null);
            nativeAdData.setNativeAdEventListener(null);

            if (nativeAdData.isExpressAd()) {
                view = nativeAdData.getFeedView();
            } else {
                view = createNativeAdView(nativeAdData, adViewHolder);
            }

            adViewHolder.adContainer.removeAllViews();

            if (view != null) {
                UIUtil.removeViewFromParent(view);
                adViewHolder.adContainer.addView(view);
            }
        } else if (holder instanceof CardViewHolder) {
            CardViewHolder cardViewHolder = (CardViewHolder) holder;
            ViewHolderDataBinder.bindCardContent(cardViewHolder, position);
        } else if (holder instanceof ImageViewHolder) {
            ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
            ViewHolderDataBinder.bindImageContent(imageViewHolder, position);
        } else if (holder instanceof VideoViewHolder) {
            VideoViewHolder videoViewHolder = (VideoViewHolder) holder;
            ViewHolderDataBinder.bindVideoContent(videoViewHolder, position);
        } else if (holder instanceof NewsViewHolder) {
            NewsViewHolder newsViewHolder = (NewsViewHolder) holder;
            ViewHolderDataBinder.bindNewsContent(newsViewHolder, position);
        } else if (holder instanceof ProductViewHolder) {
            ProductViewHolder productViewHolder = (ProductViewHolder) holder;
            ViewHolderDataBinder.bindProductContent(productViewHolder, position);
        } else if (holder instanceof NormalViewHolder) {
            NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
            normalViewHolder.text.setText("Recycler item " + position);
        }
    }

    private View createNativeAdView(NativeAdData nativeAdData, AdViewHolder adViewHolder) {
        // 广告交互监听
        return adViewHolder.adRender.renderAdView(nativeAdData, new NativeAdEventListener() {
            @Override
            public void onAdExposed() {
                Log.d(ConstantsDemo.LOG_TAG, "----------onAdExposed----------");
            }

            @Override
            public void onAdClicked() {
                Log.d(ConstantsDemo.LOG_TAG, "----------onAdClicked----------");
            }

            @Override
            public void onAdRenderFail(AdError error) {
                Log.d(ConstantsDemo.LOG_TAG, "----------onAdRenderFail----------:" + error.toString());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        NativeAdData ad = mData.get(position);
        if (ad == null) {
            // 根据位置返回不同的普通内容类型
            int normalType = position % 5;
            switch (normalType) {
                case 0:
                    return ITEM_VIEW_TYPE_CARD;
                case 1:
                    return ITEM_VIEW_TYPE_IMAGE;
                case 2:
                    return ITEM_VIEW_TYPE_VIDEO;
                case 3:
                    return ITEM_VIEW_TYPE_NEWS;
                default:
                    return ITEM_VIEW_TYPE_NORMAL;
            }
        } else {
            return ITEM_VIEW_TYPE_AD;
        }
    }

}
